package com.jnu.myaccount.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jnu.myaccount.DetailActivity;
import com.jnu.myaccount.R;
import com.jnu.myaccount.data.Data;
import com.jnu.myaccount.data.ShowListItem;
import com.jnu.myaccount.function.home.HomeFragment;

import java.util.ArrayList;

public class ShowListViewAdapter extends ArrayAdapter {


    private int ImageId;
    private ArrayList<Data> mList = new ArrayList<Data>();//定义以Data实体类为对象的数据集合
    private  ArrayList<ShowListItem> mItemList=new ArrayList<ShowListItem>();


    public ShowListViewAdapter(Context context, int ImageId, ArrayList<ShowListItem> obj) {
        super(context, ImageId, obj);
        this.ImageId = ImageId;
        this.mItemList=obj ;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ShowListItem mItem = (ShowListItem) getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(ImageId, null);//这个是实例化一个我们自己写的界面Item
        TextView mDate = view.findViewById(R.id.showItemDate);//日期
        TextView mIncome = view.findViewById(R.id.showItemIncome);//收入
        TextView mPaid = view.findViewById(R.id.showItemPaid);//支出
        RecyclerView mRecycle = view.findViewById(R.id.showItemRecycle);//此月份的recycleview列表
        mDate.setText(mItem.getDate());
        mIncome.setText(mItem.getIncome() + "元");
        mPaid.setText(mItem.getPaid() + "元");

        //recycleView初始化
        mList=mItem.getList();

        //创建adapter
        HomeFragment.CollectRecycleAdapter mCollectRecyclerAdapter = new HomeFragment.CollectRecycleAdapter(getContext(), mList);
        //给RecyclerView设置adapter
        int spacingInPixels = 8;
        mRecycle.addItemDecoration(new mSpacesItemDecoration(spacingInPixels));

        mRecycle.setAdapter(mCollectRecyclerAdapter);
        //RecycleView 增加边距

        //设置layoutManager,可以设置显示效果，是线性布局、grid布局，还是瀑布流布局
        //参数是：上下文、列表方向（横向还是纵向）、是否倒叙
        mRecycle.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        //设置item的分割线
        mRecycle.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));

        //RecyclerView中没有item的监听事件，需要自己在适配器中写一个监听事件的接口。参数根据自定义
        mCollectRecyclerAdapter.setOnItemClickListener(new HomeFragment.CollectRecycleAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, Data data, int position) {
                //此处进行监听事件的业务处理
                Toast.makeText(getContext(), data.getItemName(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getContext(), DetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("type", "show");//表明这是一条用来展示的intent
                bundle.putSerializable("DataInfo", data);
                bundle.putInt("position", position);
                intent.putExtras(bundle);
                getContext().startActivity(intent);
            }
        });
        return view;
    }
        public void setmList(ArrayList<ShowListItem> newList){
        this.mItemList=newList;
}

    private class mSpacesItemDecoration extends RecyclerView.ItemDecoration {

        private int space;

        public mSpacesItemDecoration(int space) {
            this.space = space;
        }
    }

}
